package com.jh.user.service;

import cn.jh.common.pojo.BaseResponse;
import cn.jh.common.pojo.ValidationUtils;
import com.jh.user.business.ChannelConfigBusiness;
import com.jh.user.pojo.ChannelConfig;
import com.jh.user.pojo.validator.Create;
import com.jh.user.util.CommonsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@EnableAutoConfiguration
public class ChannelConfigService {

    private  Logger LOG = LoggerFactory.getLogger(ChannelConfigService.class);

    @Autowired
    private ChannelConfigBusiness channelConfigBusiness;


    /**
     *
     * 根据终端号查询
     */
    @RequestMapping(method=RequestMethod.GET,value="/v1.0/user/channelConfig/findByTerminalNo")
    public @ResponseBody
    Object findByTerminalNo(String terminalNo){
        return BaseResponse.success(channelConfigBusiness.findByTerminalNo(terminalNo));
    }

    /**
     *
     * 新增费率
     */
    @RequestMapping(method=RequestMethod.POST,value="/v1.0/user/channelConfig/add")
    public @ResponseBody Object add(ChannelConfig channelConfig){
        try {
            ValidationUtils.validData(channelConfig,Create.class);
            channelConfig.setCreateTime(new Date());
            channelConfigBusiness.save(channelConfig);
            return BaseResponse.success();
        }catch (Exception e){
            LOG.error("ChannelConfigService.add, 新增费率异常=" + e);
            return CommonsUtil.returnError("新增费率失败");
        }
    }

    /**
     *
     * 修改费率
     */
    @RequestMapping(method=RequestMethod.POST,value="/v1.0/user/channelConfig/update")
    public @ResponseBody
    Object update(ChannelConfig channelConfig){
        if (channelConfig.getId() == null ) return BaseResponse.error("id不能为空");
        channelConfigBusiness.save(channelConfig);
        return BaseResponse.success();
    }

}
